<?php

namespace App\Http\Controllers;

use App\Models\Classify;
use App\Services\CommonService;
use Illuminate\Http\Request;
use App\Services\ClassifyService;
use Illuminate\Http\JsonResponse;

class ClassifyController extends Controller
{
    public $classifyService;

    public function __construct(ClassifyService $classifyService)
    {
        parent::__construct();
        $this->classifyService = $classifyService;
    }

    /**
     * 获取添加项目分类需要的元素
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/2/1 11:24
     */
    public function getChars(Request $request): JsonResponse
    {
        $data = [
            'types'             => Classify::TP,
            'stage_codes'       => Classify::JD,
            'major_codes'       => Classify::VUE_USED_ZY,
            'early_stage_codes' => Classify::VUE_USED_EARLY_ZY,
        ];

        return $this->success($data);
    }

    /**
     * 数据列表
     */
    public function index(Request $request)
    {
        $perPage = $request->input('per_page') ?: $this->limit;

        $list = $this->classifyService->getList($request, $perPage);

        return $this->success($list);
    }

    /**
     * 保存数据
     */
    public function store(Request $request)
    {
        $id = $request->input('id');
        if (!$this->classifyService->saveData($id, $request->all())) {
            return $this->error('保存失败~');
        }
        return $this->success('', '操作成功！');
    }

    /**
     * 总分类目录树状图
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/1/31 9:22
     */
    function treeClassifies(Request $request): JsonResponse
    {
        $templateId = $request->input('template_id', 0);

        $treeClassifies = $this->classifyService->treeClassifies($templateId);

        return $this->success($treeClassifies);
    }

    /**
     * 分类目录设置或取消单个参建单位
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/1/31 15:06
     */
    public function setPartCompanyType(Request $request): JsonResponse
    {
        $classifyId  = $request->input('id');
        $status      = $request->input('status');
        $companyType = $request->input('part_company_type');
        $templateId  = $request->input('template_id');
        $result      = $this->classifyService->partCompanyStatus($templateId, $classifyId, $companyType, $status);
        if ($result['msg']) {
            return $this->error($result['msg']);
        }
        return $this->success('', '操作成功！');
    }

    /**
     * 回收站列表（进行软删除的数据）
     */
    public function recycle(Request $request)
    {
        $data = $this->classifyService->classifyRecycle($request);

        return $this->success($data);
    }

    /**
     * 单个软删除 （软删除相关的全部是Laravel内置操作方法）
     */
    public function destroy(Request $request)
    {
        $id = $request->input('id');
        if (!Classify::query()->where('id', $id)->delete()) {
            return $this->error('删除失败！');
        };
        CommonService::addActionLog('删除分类', "数据ID:{$id}");

        return $this->success('', '删除成功');
    }

    /**
     * 批量软删除，ids可以是以","逗号分割的字符串，也可以是数组形式
     */
    public function destroyMore(Request $request)
    {
        $ids = to_array($request->input('ids'));
        if (!Classify::destroy($ids)) {
            return $this->error('删除失败！');
        };
        $ids = implode(',', $ids);
        CommonService::addActionLog('删除分类', "数据ID:{$ids}");

        return $this->success('', '放入回收站成功');
    }

    /**
     * 恢复单个数据
     */
    function retrieve(Request $request)
    {
        $id = $request->input('id', 0);
        if (!Classify::query()->where('id', $id)->restore()) {
            return $this->error('恢复失败！');
        }
        CommonService::addActionLog('恢复分类', "数据ID:{$id}");

        return $this->success('', '恢复成功');
    }

    /**
     * 恢复批量数据
     */
    function retrieveMore(Request $request)
    {
        $ids = to_array($request->input('ids'));
        if (!Classify::query()->whereIn('id', $ids)->restore()) {
            return $this->error('恢复失败！');
        }
        $ids = implode(',', $ids);
        CommonService::addActionLog('删除分类', "数据ID:{$ids}");

        return $this->success('', '恢复成功');
    }

    /**
     * 永久删除一条数据
     */
    public function delete(Request $request)
    {
        $id = $request->input('id', 0);
        if (!Classify::withTrashed()->find($id)->forceDelete()) {
            return $this->error('删除失败！');
        }
        CommonService::addActionLog('删除分类', "数据ID:{$id}");

        return $this->success('', '删除成功');
    }

    /**
     * 永久删除批量数据，ids可以是以","逗号分割的字符串，也可以是数组形式
     */
    public function deleteMore(Request $request)
    {
        $ids = to_array($request->input('ids'));
        if (!Classify::withTrashed()->whereIn('id', $ids)->forceDelete()) {
            return $this->error('删除失败！');
        }
        $ids = implode(',', $ids);
        CommonService::addActionLog('删除分类', "数据ID:{$ids}");

        return $this->success('', '删除成功');
    }

}
